package com.ijunfu.webSocket;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ijunfu.common.utils.JsonUtil;
import com.ijunfu.common.utils.R;
import lombok.extern.slf4j.Slf4j;

import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;

/**
 *
 * @Title  : 
 * @Author : ijunfu <ijunfu@163.com>
 * @Date   : 2024/2/6 23:11
 * @Version: 1.0
 * @Motto  : 简洁的代码是智慧的结晶 卓越的编码是对复杂性的优雅征服
 *
 */
@Slf4j
public class WsEncoder implements Encoder.Text<R> {

    @Override
    public String encode(R r) throws EncodeException {
        assert r != null;

        try {
            return JsonUtil.toJson(r);
        } catch (JsonProcessingException e) {
            log.error("format websocket message fail: msg={} err={}", r, e.getMessage());

            throw new EncodeException(r, e.getMessage());
        }
    }

    @Override
    public void init(EndpointConfig endpointConfig) {

    }

    @Override
    public void destroy() {

    }
}
